package com.example.rocketmq.timewheel.mq;

import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.rocketmq.timewheel.model.ScheduledTask;
import com.example.rocketmq.timewheel.service.TaskExecuteService;

import lombok.extern.slf4j.Slf4j;

/**
 * RocketMQ配置
 */
@Configuration
@Slf4j
public class RocketMQConfig {
    
    // 定时任务消息主题
    public static final String SCHEDULED_TASK_TOPIC = "ScheduledTaskTopic";
    
    /**
     * 定时任务消费者
     */
    @Bean
    public RocketMQListener<ScheduledTask> scheduledTaskListener(TaskExecuteService taskExecuteService) {
        return new RocketMQListener<ScheduledTask>() {
            @Override
            public void onMessage(ScheduledTask task) {
                log.info("接收到定时任务: {}", task);
                // 执行任务
                taskExecuteService.executeTask(task);
            }
        };
    }
    
    /**
     * 定时任务消费者监听器配置
     */
    @RocketMQMessageListener(
            topic = SCHEDULED_TASK_TOPIC,
            consumerGroup = "${rocketmq.consumer.group}",
            messageModel = MessageModel.CLUSTERING,
            consumeMode = ConsumeMode.CONCURRENTLY
    )
    @Slf4j
    public static class ScheduledTaskConsumer {}
}
